package com.edu.exam.entity.curriculum.questionbank;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 试题分类关联实体类
 * 所属模块：B-02 题库管理模块
 * 对应数据库表：question_category_assoc
 *
 *
 * @author System
 * @version 1.0.0
 */
@Entity
@Comment("试题分类关联表")
@SQLDelete(sql = "UPDATE question_category_assoc SET is_deleted = true, delete_time = CURRENT_TIMESTAMP WHERE id = ?")
@Where(clause = "is_deleted = false")
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class QuestionCategoryAssoc {

    /**
     * 关联记录唯一标识符
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    /**
     * 试题ID
     */
    @NotNull(message = "试题ID不能为空")
    @Column(name = "question_id", nullable = false)
    private Long questionId;

    /**
     * 分类ID
     */
    @NotNull(message = "分类ID不能为空")
    @Column(name = "category_id", nullable = false)
    private Long categoryId;

    /**
     * 关联类型
     */
    @NotNull(message = "关联类型不能为空")
    @Enumerated(EnumType.STRING)
    @Column(name = "association_type", nullable = false, length = 20)
    private AssociationType associationType;

    /**
     * 关联权重
     */
    @NotNull(message = "关联权重不能为空")
    @Column(name = "weight", nullable = false, precision = 3, scale = 2)
    @Builder.Default
    private BigDecimal weight = BigDecimal.ONE;

    /**
     * 关联强度
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "association_strength", length = 20)
    private AssociationStrength associationStrength;

    /**
     * 关联说明
     */
    @Column(name = "association_description", length = 500)
    private String associationDescription;

    /**
     * 是否主分类
     */
    @Column(name = "is_primary")
    @Builder.Default
    private Boolean isPrimary = false;

    /**
     * 关联来源
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "association_source", length = 20)
    private AssociationSource associationSource;

    /**
     * 关联置信度
     */
    @Column(name = "confidence_level", precision = 3, scale = 2)
    private BigDecimal confidenceLevel;

    /**
     * 关联优先级
     */
    @Column(name = "priority")
    @Builder.Default
    private Integer priority = 0;

    /**
     * 关联标签
     */
    @Column(name = "association_tags", length = 200)
    private String associationTags;

    /**
     * 关联元数据，JSON格式
     */
    @Column(name = "metadata", columnDefinition = "TEXT")
    private String metadata;

    /**
     * 记录创建时间
     */
    @Column(name = "created_time", nullable = false, updatable = false)
    private LocalDateTime createdTime;

    /**
     * 创建人用户ID
     */
    @Size(max = 50, message = "创建人长度不能超过50个字符")
    @Column(name = "created_by", nullable = false, length = 50)
    private String createdBy;

    /**
     * 乐观锁版本号
     */
    @Version
    @Column(name = "version", nullable = false)
    @Builder.Default
    private Integer version = 1;

    /**
     * 软删除标记
     */
    @Column(name = "is_deleted", nullable = false)
    @Builder.Default
    private Boolean isDeleted = false;

    /**
     * 删除时间
     */
    @Column(name = "delete_time")
    private LocalDateTime deleteTime;

    @PrePersist
    protected void onCreate() {
        LocalDateTime now = LocalDateTime.now();
        this.createdTime = now;
    }

    /**
     * 关联类型枚举
     */
    public enum AssociationType {
        PRIMARY("PRIMARY", "主分类"),
        SECONDARY("SECONDARY", "次分类"),
        TERTIARY("TERTIARY", "第三分类"),
        RELATED("RELATED", "相关分类"),
        CROSS_SUBJECT("CROSS_SUBJECT", "跨学科分类"),
        REFERENCE("REFERENCE", "参考分类");

        private final String code;
        private final String description;

        AssociationType(String code, String description) {
            this.code = code;
            this.description = description;
        }

        public String getCode() {
            return code;
        }

        public String getDescription() {
            return description;
        }

        public static AssociationType fromCode(String code) {
            for (AssociationType type : values()) {
                if (type.getCode().equals(code)) {
                    return type;
                }
            }
            return PRIMARY;
        }
    }

    /**
     * 关联强度枚举
     */
    public enum AssociationStrength {
        WEAK("WEAK", "弱关联"),
        MEDIUM("MEDIUM", "中等关联"),
        STRONG("STRONG", "强关联"),
        CRITICAL("CRITICAL", "关键关联");

        private final String code;
        private final String description;

        AssociationStrength(String code, String description) {
            this.code = code;
            this.description = description;
        }

        public String getCode() {
            return code;
        }

        public String getDescription() {
            return description;
        }

        public static AssociationStrength fromCode(String code) {
            for (AssociationStrength strength : values()) {
                if (strength.getCode().equals(code)) {
                    return strength;
                }
            }
            return MEDIUM;
        }
    }

    /**
     * 关联来源枚举
     */
    public enum AssociationSource {
        MANUAL("MANUAL", "手动关联"),
        AUTO_CLASSIFY("AUTO_CLASSIFY", "自动分类"),
        SYSTEM("SYSTEM", "系统关联"),
        AI("AI", "智能关联"),
        IMPORT("IMPORT", "导入关联"),
        MIGRATION("MIGRATION", "迁移关联");

        private final String code;
        private final String description;

        AssociationSource(String code, String description) {
            this.code = code;
            this.description = description;
        }

        public String getCode() {
            return code;
        }

        public String getDescription() {
            return description;
        }

        public static AssociationSource fromCode(String code) {
            for (AssociationSource source : values()) {
                if (source.getCode().equals(code)) {
                    return source;
                }
            }
            return MANUAL;
        }
    }
}